## -*- coding: utf-8 -*-
<%inherit file="/base/blog_base.html"/>
<div id="main">
  <h3>创建新文章</h3>
  ${h.form(h.url_for(controller='post', action='save', sub_domain=c.userInfo.userName), method='post', id='blog_form')}
    <table width="100%">
      <tbody>
        <tr><td>${h.hidden('id')} ${h.hidden('abstract')}</td><td></td></tr>
        <tr>
          <td class="row1">主题</td>
          <td> ${h.text('title', class_='text', style='width: 400px;')}</td>
        </tr>        
        <tr>
          <td class="row1">文章分类</td>
          <td>${h.select("type", 1, [[1, u"原创"], [2, u"转载"], [3, u"新闻"], [4, u"翻译"]])}
          <%!
            from pythonblog.service.section_service import all_sections
          %>
          %if not c.is_posted:    
            ${h.select("section_id", 1, ((section.id, section.name) for section in all_sections.values()))}
          %else:
            ${h.select("section_id", 1, ((section.id, section.name) for section in all_sections.values()),  onbeforeactivate="return false", onfocus="this.blur()", onmouseover="this.setCapture()", onmouseout="this.releaseCapture()")}
          %endif
          </td>
        </tr>
        <tr>
          <td class="row1">文章标签<span style="color: red; margin-left: 5px; font-weight: normal;">(用逗号隔开) </span> </td>
          <td>${h.text('tags', class_='text', style='width: 250px;')}</td>
        </tr>
        <tr>
          <td class="row1">正文 </td>
          <td></td>
        </tr>
        <tr>
          <td style="border-top: 1px solid rgb(204, 204, 204);" colspan="2"> </td>
        </tr>
        <tr><td></td><td></td></tr>
        <tr>
          <td colspan="2" style="" id="editWrapper">${h.textarea('content', '', style='width:680px;height:400px;')}</td>
        </tr>
        <tr>
          <td colspan="2" style="padding-top:10px;"> 提示：内容长度最大64KB，约2万多汉字，如果出现超长（比如从Word粘贴带格式文本）被截断的问题，请分开发布 </td>
        </tr>
        <tr>
          <td colspan="2" style="padding:15px 0px 25px 0px">
          	<input id="btnSubmit" class="submit" type="button" value="发布" />
          	%if not c.is_posted:
          	<input id="btnSaveDraft" class="submit" type="button" value="存为草稿" />
          	%endif
          </td>
        </tr>
      </tbody>
    </table>
  ${h.end_form()}
</div>

<%def name="head()">
  ${parent.head()}
  ${h.javascript_link(h.url_for('/js/jquery.form.js'))}
  ${h.javascript_link(h.url_for('/js/jquery.validate.min.js'))}
  ${h.javascript_link(h.url_for('/js/nicEdit.js'))}
  
  <script type="text/javascript">
  var contentTextArea;
  $(document).ready(function() {
	  $("#blog_form").validate({
		onsubmit: false,
		rules: {
		  title: {
		    required: true,
			rangelength: [1, 50]
		  },
		  content: {
			required: true,
			rangelength: [1, 50000]
		  }
	    },
		messages: {
	      title: {
		    required: "请提供标题",
		    rangelength: jQuery.format("请输入{0}到{1}个文字")
          },
          content: {
  		    required: "请提供文章内容",
  		    rangelength: jQuery.format("请输入{0}到{1}个文字")
          }
		},
		errorPlacement: function(error, element) {
			error.appendTo( element.parent("td").parent("tr").prev("tr").children("td")[1] );
			error.delay(3000, function() {
				error.remove();
			});
			//error.appendTo($('#errorWrapper'));
		},
		// set this class to error-labels to indicate valid fields
		success: function(label) {
		  // set &nbsp; as text for IE
		  //label.html("&nbsp;").addClass("checked").appendTo(label.parent("td").parent("tr").prev("tr").children("td")[1]);
		  label.remove();
		}
	  });

	  $('#btnSubmit').click(function(e) {
		  setValues();
		  if ($("#blog_form").validate().form()) {
			    $("#blog_form").submit();		
			}
	  });

	  

	  $('#btnSaveDraft').click(function(e) {
		setValues();
		if ($("#blog_form").validate().element("#title")) {
		  $("#blog_form").attr("action","${h.url_for(controller='post', action='save_draft', sub_domain=c.userInfo.userName)}");
		  $("#blog_form").submit();		
		}	
	  });
	  
	  contentTextArea = new nicEditor({maxHeight : 400, 
		  iconsPath : "${h.url_for('/js/nicEditorIcons.gif')}",
		  buttonList : ['bold','italic','underline','left','center','right','ol','ul','fontSize','indent','outdent','image','link','unlink','forecolor']
			}).panelInstance('content');
	  
  });

  function setValues() {
	  var data = contentTextArea.nicInstances[0].getContent(); 
	  if ('<br>' == data.toLowerCase()) {
		  data = '';
	  }
	  $('[name=content]').val(data);
	  data = data.replace(/<br[\s\/]{0,2}>/ig, "\r\n");
	  data = data.replace(/<.*?>/ig, "");
      data = data.replace(/&nbsp;/ig, "");
      data = data.substring(0, 100);
      //TODO: consider how many words to get
	  $('[name=abstract]').val(data + '...');
  }
  	
  </script>	
</%def>